<template>
  <svg :class="[prefixCls, $attrs.class, spin && 'svg-icon-spin']" :style="getStyle" aria-hidden="true">
    <use :xlink:href="symbolId" />
  </svg>
</template>

<script setup lang="ts" name="SvgIcon">
import type { PropType } from 'vue'
import { computed } from 'vue'
import { propTypes } from '@/utils/propTypes'
// import { prefixCls } from '@/settings/designSetting'

const prefixCls = 'svg-icon'
const props = defineProps({
  // name
  name: propTypes.string,
  // icon size
  size: {
    type: [String, Number] as PropType<string | number>,
    default: 16,
  },
  spin: propTypes.bool.def(false),
  prefix: propTypes.string.def('icon'),
})
const symbolId = computed(() => `#${props.prefix}-${props.name}`)

// TODO 这里使用  可选链 ?.就会报错
// const symbolId = computed(() => `#${props?.prefix}-${props.name}`)

const getStyle = computed((): any => {
  const { size } = props
  let s = `${size}`
  s = `${s.replace('px', '')}px`
  return {
    width: s,
    height: s,
  }
})
</script>

<style lang="less" scoped>
//   @prefix-cls: ~'@{namespace}-svg-icon';

// .@{prefix-cls} {
.svg-icon {
  display: inline-block;
  overflow: hidden;
  vertical-align: -0.15em;
  fill: currentColor;
}

.svg-icon-spin {
  animation: loadingCircle 1s infinite linear;
}
</style>
